using UnityEngine;
using System.Collections;

public class CameraInteract : MonoBehaviour {

    public float detectionLength = 1f;
    public GUIText text;

    private RaycastHit hitInfo = new RaycastHit();

    void Start () {
        gameObject.AddComponent<Rigidbody>();
        Rigidbody rigidbody = gameObject.GetComponent<Rigidbody>();
        rigidbody.useGravity = false;
        rigidbody.isKinematic = true;
    }

	void LateUpdate () {

        bool detectSomething = Physics.Raycast(gameObject.transform.position, transform.forward, out hitInfo, detectionLength);

        if (text != null)
            text.text = detectSomething ? hitInfo.collider.gameObject.name : "" + gameObject.transform.position + transform.forward;

        if (detectSomething)
        {
            InteractObject interactObject = hitInfo.collider.gameObject.GetComponent<InteractObject>();
            if (interactObject != null)
            {
                if (text != null)
                    text.text += interactObject.GetMessage();

                interactObject.Interact(this);
            }
        }
	}
}
